package pku.tcs.wsc.service;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @author Lilei
 * 
 *
 */

public class ServicePool {
	//<serviceid,Service>
	public HashMap<String,Service> pool = new HashMap<String, Service>();	
	
	//put service in the pool, invoked by wsdl parser...
	public void addService(Service s)throws Exception{
		if(pool.containsKey(s.id))
			throw new Exception("Service "+s.id+" has been added in the pool yet!!!!");
		pool.put(s.id, s);
	}
	
	//add qos info to the pool, invoked by wsla parser...
	public void addServiceQos(String service_id,long response_qos,float throughput_qos)throws Exception{
		//if(!pool.containsKey(service_id))
		///	throw new Exception(service_id + "is not contained in the pool!!!!!");
		//there are services not contained in the services.wsdl...
		if(!pool.containsKey(service_id))
			return;
		pool.get(service_id).responseTime = response_qos;
		pool.get(service_id).throughput = throughput_qos;
	}
	
	public void writeParseResult(String resultFilePath) throws Exception{
		
		 File out = new File(resultFilePath);
		 DataOutputStream dataOut = new DataOutputStream(
				 							new BufferedOutputStream(new FileOutputStream(out)));
		
		 for (Map.Entry<String, Service> entry:pool.entrySet()){
			 Service s = entry.getValue();
			 //String  id = entry.getKey();
			 
			 dataOut.writeBytes("Service id: "+s.id+"\r\n");
			 
			 dataOut.writeBytes("input instances: \r\n");
			 for(String input:s.inputInstances){
				 dataOut.writeBytes("\t"+input+"\r\n");
			 }
			 
			 dataOut.writeBytes("output instances: \r\n");
			 for(String input:s.outputInstances){
				 dataOut.writeBytes("\t"+input+"\r\n");
			 }
			 
			 dataOut.writeBytes("Qos Information \r\n");
			 dataOut.writeBytes("\tResponse Time Qos: "+s.responseTime+"\r\n");
			 dataOut.writeBytes("\tResponse Time Qos: "+s.throughput+"\r\n");
			 
			 dataOut.writeBytes("\n");
		 }
			
		 dataOut.close();
	}
	
}
